package com.leon.programPeral.ch2.ext3;

import java.util.ArrayList;
import java.util.List;

public class Ch2Ext3ChangeStringThreeTimeClient
{
	public static void main(String[] args)
	{
		String str = WordsUtil.getRandomStr(1000000);
		int i = str.length() / 4; // 从第X个开始
		long time = System.currentTimeMillis();
		String reverse = reverse(str.substring(0, i));

		String reverse1 = reverse(str.substring(i));
		String reverse2 = reverse(reverse + reverse1);
		System.out.println(System.currentTimeMillis() - time);
//		System.out.println(reverse2);
	}

	/**
	 * 翻转一个字符串
	 * @param str 原码
	 * @return 翻转后的
	 */
	public static String reverse(String str)
	{
		int i = 0;
		int j = str.length() - 1;
		List<Character> list = new ArrayList<>(str.length());
		for (int k = 0; k < str.length(); k++)
		{
			list.add(str.charAt(k));
		}
		while (j > i)
		{
			Character c = list.get(i);
			list.set(i, list.get(j));
			list.set(j, c);
			i++;
			j--;
		}
		StringBuilder sb = new StringBuilder();
		list.stream().forEach((a) -> sb.append(a));
		return sb.toString();
	}
}
